package com.qinyu.treehole.controller;

import com.qinyu.treehole.annotation.UserAuth;
import com.qinyu.treehole.bo.CommentBo;
import com.qinyu.treehole.entities.Comment;
import com.qinyu.treehole.entities.Feed;
import com.qinyu.treehole.entities.R;
import com.qinyu.treehole.service.CommentService;
import com.qinyu.treehole.service.FeedService;
import com.qinyu.treehole.tool.CommentTool;
import com.qinyu.treehole.tool.FeedTool;
import com.qinyu.treehole.vo.FeedCommentVo;
import com.qinyu.treehole.vo.LoginFeedCommentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/comment")
@Slf4j
@Validated
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private FeedService feedService;

    @UserAuth
    @PostMapping
    public R saveComment(@Validated CommentBo commentBo){
        log.info("用户id:{}发布评论...", commentBo.getUserId());
        // 保证feedId有效！
        Feed feed = feedService.getById(commentBo.getFeedId());
        if(feed == null){
            return R.no(null, FeedTool.FEED_NO_EXIST);
        }

        boolean f = commentService.saveComment(commentBo);
        if(f) return R.ok(null, CommentTool.COMMENT_SEND_SUCCESS);

        return R.no(null, CommentTool.COMMENT_SEND_FAIL);
    }

    @UserAuth
    @PostMapping("/like")
    public R addlikeComment(@RequestParam("userId")Integer userId,
                            @RequestParam("commentId") Integer commentId){
        log.info("用户id:{}点赞评论id:{}...", userId, commentId);
        Comment comment = commentService.getById(commentId);
        if(comment == null){
            return R.no(null, CommentTool.COMMENT_NO_EXIST);
        }

        commentService.addLike(userId, commentId);
        return R.ok(null, CommentTool.COMMENT_LIKE_SUCCESS);
    }

    @UserAuth
    @DeleteMapping("/like")
    public R removelikeComment(@RequestParam("userId")Integer userId,
                            @RequestParam("commentId") Integer commentId){
        log.info("用户id:{}取消点赞评论id:{}...", userId, commentId);
        Comment comment = commentService.getById(commentId);
        if(comment == null){
            return R.no(null, CommentTool.COMMENT_NO_EXIST);
        }

        commentService.removeLike(userId, commentId);
        return R.ok(null, CommentTool.COMMENT_UNLIKE_SUCCESS);
    }

    @GetMapping("/feed")
    public R getCommentsFromFeed(@RequestParam("feedId")Integer feedId){
        Feed feed = feedService.getById(feedId);
        if(feed == null){
            return R.no(null, FeedTool.FEED_NO_EXIST);
        }

        List<FeedCommentVo> comments = commentService.getCommentsVoByFeedId(feedId);
        return R.ok(comments, CommentTool.COMMENT_QUERY_SUCCESS);
    }

    @UserAuth
    @GetMapping("/feed/u")
    public R getCommentsLoginFromFeed(@RequestParam("feedId")Integer feedId,
                                      @RequestParam("userId")Integer userId){
        log.info("用户id:{}查询动态id:{}下所有评论...", userId, feedId);
        Feed feed = feedService.getById(feedId);
        if(feed == null){
            return R.no(null, FeedTool.FEED_NO_EXIST);
        }

        List<LoginFeedCommentVo> comments = commentService.getLoginCommentsVoByFeedId(feedId, userId);
        return R.ok(comments, CommentTool.COMMENT_QUERY_SUCCESS);
    }
}
